package com.henu.main.util;


import com.henu.main.entities.YarnApplication;

import java.sql.*;

public class ApplicationToMySQLUtil {

//    public static void insertOrUpdate(YarnApplication application) {
//        Connection connection = null;
//        PreparedStatement preparedStatementQuery = null;
//        PreparedStatement preparedStatementUpdate = null;
//        PreparedStatement preparedStatementInsert = null;
//        try {
//            Class.forName("com.mysql.jdbc.Driver");
//            connection = DriverManager.getConnection("jdbc:mysql://" + Constant.MYSQL_HOSTNAME + ":3306", Constant.MYSQL_USERNAME, Constant.MYSQL_PASSWORD);
//
//            // 查询
//            String query = "select count(*) as times from yarn_application_record.application_list where id = ?";
//            preparedStatementQuery = connection.prepareStatement(query);
//            preparedStatementQuery.setString(1, application.getId());
//            ResultSet rs = preparedStatementQuery.executeQuery();
//            rs.next();
//            long times = rs.getLong(1);
//            if (times == 0) {
//                // 需要插入
//                String insert = "insert into yarn_application_record.application_list values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
//                preparedStatementInsert = connection.prepareStatement(insert);
//                preparedStatementInsert.setString(1, application.getId());
//                preparedStatementInsert.setString(2, application.getUser());
//                preparedStatementInsert.setString(3, application.getName());
//                preparedStatementInsert.setString(4, application.getQueue());
//                preparedStatementInsert.setString(5, application.getState());
//                preparedStatementInsert.setString(6, application.getFinalStatus());
//                preparedStatementInsert.setDouble(7, application.getProgress());
//                preparedStatementInsert.setString(8, application.getTrackingUI());
//                preparedStatementInsert.setString(9, application.getTrackingUrl());
//                preparedStatementInsert.setString(10, application.getDiagnostics());
//                preparedStatementInsert.setLong(11, application.getClusterId());
//                preparedStatementInsert.setString(12, application.getApplicationType());
//                preparedStatementInsert.setDouble(13, application.getPriority());
//                preparedStatementInsert.setLong(14, application.getStartedTime());
//                preparedStatementInsert.setLong(15, application.getLaunchTime());
//                preparedStatementInsert.setLong(16, application.getFinishedTime());
//                preparedStatementInsert.setLong(17, application.getElapsedTime());
//                preparedStatementInsert.setString(18, application.getAmContainerLogs());
//                preparedStatementInsert.setString(19, application.getAmHostHttpAddress());
//                preparedStatementInsert.setString(20, application.getAmRPCAddress());
//                preparedStatementInsert.setLong(21, application.getAllocatedMB());
//                preparedStatementInsert.setLong(22, application.getAllocatedVCores());
//                preparedStatementInsert.setLong(23, application.getReservedMB());
//                preparedStatementInsert.setLong(24, application.getReservedVCores());
//                preparedStatementInsert.setLong(25, application.getRunningContainers());
//                preparedStatementInsert.setLong(26, application.getMemorySeconds());
//                preparedStatementInsert.setLong(27, application.getVcoreSeconds());
//                preparedStatementInsert.setDouble(28, application.getQueueUsagePercentage());
//                preparedStatementInsert.setDouble(29, application.getClusterUsagePercentage());
//                preparedStatementInsert.setLong(30, application.getPreemptedResourceMB());
//                preparedStatementInsert.setLong(31, application.getPreemptedResourceVCores());
//                preparedStatementInsert.setLong(32, application.getNumNonAMContainerPreempted());
//                preparedStatementInsert.setLong(33, application.getNumAMContainerPreempted());
//                preparedStatementInsert.setLong(34, application.getPreemptedMemorySeconds());
//                preparedStatementInsert.setLong(35, application.getPreemptedVcoreSeconds());
//                preparedStatementInsert.setString(36, application.getLogAggregationStatus());
//                int i = preparedStatementInsert.executeUpdate();
//                System.out.println("MySQL 写入成功操作" + i);
//            } else if (times == 1) {
//                // 需要更新
////                System.out.println("需要更新的为：");
////                System.out.println(application.toString());
////                String update = "update yarn_application_record.application_list set user = ?,name = ? where id = ?";
////                preparedStatementUpdate = connection.prepareStatement(update);
////                preparedStatementUpdate.setDouble(1, metric.getValue());
////                preparedStatementUpdate.setString(2, metric.getCluster());
////                preparedStatementUpdate.setString(3, metric.getHostname());
////                preparedStatementUpdate.setString(4, metric.getServiceName());
////                preparedStatementUpdate.setString(5, metric.getMetricName());
////                preparedStatementUpdate.setString(6, metric.getTimestamp().toString());
////                preparedStatementUpdate.executeUpdate();
////                System.out.println("已更新");
//            } else {
//                // 理论上不应该存在
//                System.out.println("理论上不应该存在" + application.toString());
//            }
//        } catch (SQLException | ClassNotFoundException e) {
//            throw new RuntimeException(e);
//        } finally {
//            try {
//                assert connection != null;
//                connection.close();
//            } catch (SQLException e) {
//                throw new RuntimeException(e);
//            }
//            try {
//                if (preparedStatementQuery != null) {
//                    preparedStatementQuery.close();
//                }
//            } catch (SQLException e) {
//                throw new RuntimeException(e);
//            }
//            try {
//                if (preparedStatementUpdate != null) {
//                    preparedStatementUpdate.close();
//                }
//            } catch (SQLException e) {
//                throw new RuntimeException(e);
//            }
//            try {
//                if (preparedStatementInsert != null) {
//                    preparedStatementInsert.close();
//                }
//            } catch (SQLException e) {
//                throw new RuntimeException(e);
//            }
//        }
//    }

}
